package com.ivy.irublesson.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.ivy.irublesson.GetConnection.Login;
import com.ivy.irublesson.GetConnection.TContentConn;
import com.ivy.irublesson.R;
import com.ivy.irublesson.entity.TContent;
import com.ivy.irublesson.entity.Teacher;
import com.ivy.irublesson.utils.HttpUtil;
import com.ivy.irublesson.utils.MyOpenHelper;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TContentActivity extends AppCompatActivity {
    private TextView tv_11,tv_12,tv_13,tv_14,tv_15,tv_16,tv_17;
    private TextView tv_21,tv_22,tv_23,tv_24,tv_25,tv_26,tv_27;
    private TextView tv_31,tv_32,tv_33,tv_34,tv_35,tv_36,tv_37;
    private TextView tv_41,tv_42,tv_43,tv_44,tv_45,tv_46,tv_47;
    private TextView tv_51,tv_52,tv_53,tv_54,tv_55,tv_56,tv_57;
    private TextView tv_61,tv_62,tv_63,tv_64,tv_65,tv_66,tv_67;

    private List<TContent> tcontents = new ArrayList<TContent>();
    private TextView arr[][]=new TextView[6][7];

//    private ImageView iv_yzm;
    ImageView imageView1,imageView2;
    EditText editText;

//    private ImageView iv_search;

    private Spinner sp;
    private List<String> list;

    private String valPath = "http://1.85.55.152/sys/ValidateCode.aspx?t=807";
    private String referer="http://1.85.55.152/ZNPK/TeacherKBFB.aspx";
    private String cookie;


    private String tid;
    private MyOpenHelper openHelper;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tcontent);
        tid = getIntent().getStringExtra("tid");

        //隐藏
        imageView1 = (ImageView) findViewById(R.id.tb);
        imageView2 = (ImageView) findViewById(R.id.tyzm);

        editText = (EditText) findViewById(R.id.tinput);
        sp = (Spinner) findViewById(R.id.tsp);

        initLeft();
        initTextView();

        Thread cookieThread = new Thread(new CookieThread());
        cookieThread.start();

        //courses = CourseConn.parseCourse();
        //initData();


    }

    public void changeVal(View view) {
        Thread cookieThread = new Thread(new CookieThread());
        cookieThread.start();
    }
    public void search(View view) {
//        if(true ){
//            imageView1.setVisibility(View.GONE);
//            imageView2.setVisibility(View.GONE);

            Thread courseThread = new Thread(new CourseThread());
            courseThread.start();


//            editText.setText("验证码输入正确！！！");
//        }else {
//            Toast.makeText(TContentActivity.this,"验证码输入错误！！！",Toast.LENGTH_LONG).show();
//        }



    }

    private String coursepath = "http://119.23.232.105:8080/IRubLesson/TContentServlet";
    class CourseThread implements Runnable {

        @Override
        public void run() {
            tcontents = TContentConn.getTContent(getIntent().getStringExtra("tid") , editText.getText().toString().trim() , cookie);

//            Map<String,String> params = new HashMap<String,String>();
//            params.put("opreation" , "getTContent");
//            params.put("tid" , tid);
//            params.put("valCode" , editText.getText().toString().trim());
//            JSONArray arr = null;
//            try {
//                arr = HttpUtil.postJSONArrayByURL(coursepath , params);
//                for(int i = 0;i < arr.length();i++) {
//                    JSONObject obj = arr.getJSONObject(i);
//                    TContent tcontent = new TContent();
//                    tcontent.setInfo(obj.getString("info"));
//                    tcontent.setTid(obj.getString("tid"));
//                    tcontent.setWeekday(obj.getInt("weekday"));
//                    tcontent.setTime(obj.getInt("time"));
//                    tcontents.add(tcontent);
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    adapterData();
                }
            });

        }
    }

    private ByteArrayOutputStream stream;
    private String cookiepath = "http://119.23.232.105:8080/IRubLesson/TContentServlet";
    class CookieThread implements Runnable {
        public void run() {
            cookie = Login.login(valPath,referer);
            stream = Login.getStream();


            try {
//                stream = HttpUtil.postByHttpClient(cookiepath  , "getCookie");

                byte[] buf = stream.toByteArray();
               final Bitmap bitmap = BitmapFactory.decodeByteArray(buf , 0 , buf.length);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        imageView2.setImageBitmap(bitmap);

                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    private void adapterData(){
        int x,y;
        for (final TContent tcontent : tcontents){
            x = tcontent.getTime();
            y = tcontent.getWeekday();
            for(int i = 0;i < 6; i++){
                if(x == i){
                    for (int j = 0 ; j < 7; j++){
                        if (j == y){
                            arr[i][j].setText(tcontent.getInfo());
                            //课程信息弹框显示
                            arr[i][j].setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    if(tcontent.getInfo().length() > 5) {
                                        //定义AlertDialog.Builder对象，对话框
                                        AlertDialog.Builder builder = new AlertDialog.Builder(TContentActivity.this);

                                        builder.setTitle("课表信息");
                                        builder.setMessage("星期  "+ (tcontent.getWeekday()+1) +"  ,  第  "+(tcontent.getTime()+1)+" 节                        课程名： " +tcontent.getInfo()+" ");
                                        builder.create().show();
                                    }
                                }
                            });
                        }else{
                            continue;
                        }
                    }
                }else {
                    continue;
                }

            }
        }

    }
    private void initTextView(){
        tv_11 = (TextView) findViewById(R.id.tv_11);
        tv_12 = (TextView) findViewById(R.id.tv_12);
        tv_13 = (TextView) findViewById(R.id.tv_13);
        tv_14 = (TextView) findViewById(R.id.tv_14);
        tv_15 = (TextView) findViewById(R.id.tv_15);
        tv_16 = (TextView) findViewById(R.id.tv_16);
        tv_17 = (TextView) findViewById(R.id.tv_17);
        arr[0][0] = tv_11;
        arr[0][1] = tv_12;
        arr[0][2] = tv_13;
        arr[0][3] = tv_14;
        arr[0][4] = tv_15;
        arr[0][5] = tv_16;
        arr[0][6] = tv_17;
        tv_21 = (TextView) findViewById(R.id.tv_21);
        tv_22 = (TextView) findViewById(R.id.tv_22);
        tv_23 = (TextView) findViewById(R.id.tv_23);
        tv_24 = (TextView) findViewById(R.id.tv_24);
        tv_25 = (TextView) findViewById(R.id.tv_25);
        tv_26 = (TextView) findViewById(R.id.tv_26);
        tv_27 = (TextView) findViewById(R.id.tv_27);
        arr[1][0] = tv_21;
        arr[1][1] = tv_22;
        arr[1][2] = tv_23;
        arr[1][3] = tv_24;
        arr[1][4] = tv_25;
        arr[1][5] = tv_26;
        arr[1][6] = tv_27;
        tv_31 = (TextView) findViewById(R.id.tv_31);
        tv_32 = (TextView) findViewById(R.id.tv_32);
        tv_33 = (TextView) findViewById(R.id.tv_33);
        tv_34 = (TextView) findViewById(R.id.tv_34);
        tv_35 = (TextView) findViewById(R.id.tv_35);
        tv_36 = (TextView) findViewById(R.id.tv_36);
        tv_37 = (TextView) findViewById(R.id.tv_37);
        arr[2][0] = tv_31;
        arr[2][1] = tv_32;
        arr[2][2] = tv_33;
        arr[2][3] = tv_34;
        arr[2][4] = tv_35;
        arr[2][5] = tv_36;
        arr[2][6] = tv_37;
        tv_41 = (TextView) findViewById(R.id.tv_41);
        tv_42 = (TextView) findViewById(R.id.tv_42);
        tv_43 = (TextView) findViewById(R.id.tv_43);
        tv_44 = (TextView) findViewById(R.id.tv_44);
        tv_45 = (TextView) findViewById(R.id.tv_45);
        tv_46 = (TextView) findViewById(R.id.tv_46);
        tv_47 = (TextView) findViewById(R.id.tv_47);
        arr[3][0] = tv_41;
        arr[3][1] = tv_42;
        arr[3][2] = tv_43;
        arr[3][3] = tv_44;
        arr[3][4] = tv_45;
        arr[3][5] = tv_46;
        arr[3][6] = tv_47;
        tv_51 = (TextView) findViewById(R.id.tv_51);
        tv_52 = (TextView) findViewById(R.id.tv_52);
        tv_53 = (TextView) findViewById(R.id.tv_53);
        tv_54 = (TextView) findViewById(R.id.tv_54);
        tv_55 = (TextView) findViewById(R.id.tv_55);
        tv_56 = (TextView) findViewById(R.id.tv_56);
        tv_57 = (TextView) findViewById(R.id.tv_57);
        arr[4][0] = tv_51;
        arr[4][1] = tv_52;
        arr[4][2] = tv_53;
        arr[4][3] = tv_54;
        arr[4][4] = tv_55;
        arr[4][5] = tv_56;
        arr[4][6] = tv_57;
        tv_61 = (TextView) findViewById(R.id.tv_61);
        tv_62 = (TextView) findViewById(R.id.tv_62);
        tv_63 = (TextView) findViewById(R.id.tv_63);
        tv_64 = (TextView) findViewById(R.id.tv_64);
        tv_65 = (TextView) findViewById(R.id.tv_65);
        tv_66 = (TextView) findViewById(R.id.tv_66);
        tv_67 = (TextView) findViewById(R.id.tv_67);
        arr[5][0] = tv_61;
        arr[5][1] = tv_62;
        arr[5][2] = tv_63;
        arr[5][3] = tv_64;
        arr[5][4] = tv_65;
        arr[5][5] = tv_66;
        arr[5][6] = tv_67;


    }
    private void initLeft(){
        list = new ArrayList<String>();
        list.add(getIntent().getStringExtra("tname"));

        openHelper = new MyOpenHelper(this, "irublesson.db", null, 1);
        SQLiteDatabase database = openHelper.getReadableDatabase();
        String sql = "select * from teacher where tid = "+tid+"";
        Cursor cursor = database.rawQuery(sql, null);	//	若是有问号，传入参数数组   否则为null；
        while(cursor.moveToNext()) {
            Teacher teacher = new Teacher();
            teacher.setSid(1);
            teacher.setTid(tid);
            teacher.setTname(cursor.getString(cursor.getColumnIndex("tname")));

            Log.i("======teacherNname---" ,teacher.getTname());
            list.add(teacher.getTname());
        }

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(adapter);
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(TContentActivity.this,adapter.getItem(position), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //显示
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if ( (list.get(position)) != null){
                    imageView1.setVisibility(View.VISIBLE);
                    imageView2.setVisibility(View.VISIBLE);
                    editText.setText("");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }


}
